/**
 * 
 */
package adnotatio.rdf;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import junit.framework.TestCase;

/**
 * @author kotelnikov
 */
public class EntityTest extends TestCase {

    Model fModel;

    /**
     * @param name
     */
    public EntityTest(String name) {
        super(name);
    }

    @Override
    protected void setUp() throws Exception {
        ID id = new ID("foo:bar");
        fModel = new Model(null, id);
        assertEquals(id, fModel.getID());
    }

    public void testID() {
        ID id = new ID("http://www.foo.com#bar");
        assertEquals("http://www.foo.com#", id.getNamespace());
        assertEquals("bar", id.getName());

        assertEquals("http://www.foo.com#bar", id.getURL());
        assertEquals("http://www.foo.com#bar", id.toString());
        assertEquals(id, new ID("http://www.foo.com#bar"));
        assertEquals(id, new ID("http://www.foo.com#", "bar"));

    }

    public void testResource() {
        ID id = new ID("x:y");
        Resource resource = fModel.getResource(id, false);
        assertNull(resource);
        resource = fModel.getResource(id, true);
        assertNotNull(resource);
        assertEquals(id, resource.getID());

        Reference reference = resource.getReference();
        assertNotNull(reference);
        assertEquals(id, reference.getID());

        Resource test = reference.getResource(false);
        assertNotNull(test);
        assertEquals(resource, test);
        assertEquals(1, fModel.getResourceNumber());

        resource.delete();
        test = fModel.getResource(id, false);
        assertNull(test);
        assertEquals(0, fModel.getResourceNumber());

        Set<ID> urls = new HashSet<ID>();
        int count = 1000;
        for (int i = 0; i < count; i++) {
            id = new ID("id:" + i);
            urls.add(id);
            fModel.getResource(id, true);
        }
        assertEquals(count, fModel.getResourceNumber());

        int counter = 0;
        for (Iterator<?> iterator = fModel.getResourceIterator(); iterator
            .hasNext();) {
            Resource r = (Resource) iterator.next();
            assertNotNull(r);
            assertTrue(urls.contains(r.getID()));
            counter++;
        }
        assertEquals(count, counter);
    }

    public void testResourceProperties() {
        Resource resource = fModel.getResource("x:y", true);
        resource.setValue("myprop", "ABC");
        PropertyValue value = resource.getValue("myprop");
        assertNotNull(value);
        assertTrue(value instanceof Literal);
        Literal l = (Literal) value;
        assertEquals("ABC", l.getValue());

        resource.setValue("myprop", "CDE");
        value = resource.getValue("myprop");
        assertNotNull(value);
        assertEquals("CDE", value.getValue());

        Container c = fModel.newContainer();
        c.add("ABC");
        c.add("CDE");
        resource.setValue("myprop", c);

        value = resource.getValue("myprop");
        assertNotNull(value);
        assertEquals("ABC", value.getValue());
        assertTrue(value instanceof Container);

        Container testContainer = (Container) value;
        assertEquals(c, testContainer);
    }
}
